#include "sql.hpp"
int sql_connect(MYSQL **conn)
{
    *conn = mysql_init(nullptr);
    string host = "localhost";
    string user = "xzq";
    string passwwd = "Xzq20031003zshxf.";
    string db = "disk";
    unsigned int dbport = 3306;
    if (!mysql_real_connect(*conn, host.c_str(), user.c_str(), passwwd.c_str(), db.c_str(), dbport, nullptr, 0))
    {
        printf("MYSQL connect failed!\n");
        exit(0);
    }
    else
    {
        printf("MYSQL connect succsee!\n");
    }
    return 0;
}
int find_name(MYSQL *conn, char *name, char *salt)
{
    MYSQL_RES *res;
    MYSQL_ROW row;
    char query[300] = "select salt from user where username='";
    sprintf(query, "%s%s%s", query, name, "'");
    puts(query);
    int t, ret = -1;
    t = mysql_query(conn, query);
    if (t)
    {
        printf("执行查询出错!\n");
        return ret;
    }
    else
    {
        res = mysql_use_result(conn);
        if (res)
        {
            row = mysql_fetch_row(res);
            if (row != nullptr)
            {
                if (*row[0] == 0)
                {
                    ret = -1;
                }
                else
                {
                    if (salt != nullptr)
                    {
                        strcpy(salt, row[0]);
                    }
                    ret = 0;
                }
            }
        }
        else
        {
            printf("查询出错!\n");
            exit(0);
        }
        mysql_free_result(res);
        return ret;
    }
}
// 使用SHA-256 密码哈希算法计算盐值
void get_salt(char *str)
{
    // 有问题
    str[STR_LEN] = 0;
    int i, flag;
    srand(time(NULL));
    for (i = 0; i < STR_LEN; i++)
    {
        flag = rand() % 3;
        switch (flag)
        {
        case 0:
            str[i] = rand() % 26 + 'a';
            break;
        case 1:
            str[i] = rand() % 26 + 'A';
            break;
        case 2:
            str[i] = rand() % 10 + '0';
            break;
        }
    }
    *str = '$';
    str[1] = '5';
    str[2] = '$';
    return;
}
void add_user(MYSQL *conn, char *name, char *salt, char *mima)
{
    char query[512] = "insert into user (username,salt,cryptpasswd) values (";
    sprintf(query, "%s'%s','%s','%s')", query, name, salt, mima);
    puts(query);
    int t = -1;
    t = mysql_query(conn, query);
    if (t)
    {
        printf("添加失败!\n");
    }
    else
    {
        printf("添加成功!\n");
    }
}
int math_user(MYSQL *conn, char *name, char *password, char *token)
{
    int ret = -1;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char query[300] = "select cryptpasswd from user where username='";
    sprintf(query, "%s%s%s", query, name, "'");
    int t = mysql_query(conn, query);
    if (t)
    {
        printf("执行查询出错!\n");
    }
    else
    {
        res = mysql_use_result(conn);
        if (res)
        {
            row = mysql_fetch_row(res);
            if (row != nullptr)
            {
                if (*row[0] == 0)
                {
                    ret = -1;
                }
                else if (strcmp(row[0], password) != 0)
                {
                    ret = -1;
                }
                else
                {
                    ret = 0;
                    mysql_free_result(res);
                    char query1[300] = "update user set token='";
                    char query2[300] = "where username='";
                    sprintf(query, "%s%s%s %s%s%s", query1, token, "'", query2, name, "'");
                    t = mysql_query(conn, query);
                    if (t)
                    {
                        printf("更新token值失败\n");
                    }
                    else
                    {
                        printf("更新token值成功\n");
                    }
                    return ret;
                }
            }
        }
        else
        {
            printf("查询出错\n");
            ret = -1;
        }
        mysql_free_result(res);
    }
    return ret;
}